PREFIX := /usr/local
SOFTNAME := cgpoly
VERSION := 0.0.1

OBJLIB := lib$(SOFTNAME).a
OBJSO  := lib$(SOFTNAME).so

OPTOPT := -O0 -g
#OPTOPT := -O3

all : $(OBJLIB) 

CC := gcc
CXX := g++
LINKER := g++

CFLAGS := -frounding-math $(OPTOPT)
CXXFLAGS := -frounding-math $(OPTOPT)
LDFLAGS := $(OPTOPT) 
LINKER_SO := g++ -shared 

MAKE_DEP := g++ -MM

TESTS := cgpoly_test
TESTFLAGS := -g $(OPTOPT) -I. 
TESTLIBS := -L. -l$(SOFTNAME) -lCGAL -lmpfr -lgmp

SRCS := $(wildcard *.cc)
DEPS := $(patsubst %.cc, %.d, $(wildcard *.cc))
OBJS := $(patsubst %.cc, %.o, $(SRCS))

$(OBJLIB) : $(OBJLIB)($(OBJS))

$(OBJSO)  : $(OBJS)
	$(LINKER_SO) $(LDFLAGS) -o $@ $^

sinclude $(DEPS)

%.d : %.c
	@$(MAKE_DEP) $^ 2>/dev/null | sed 's/\($*\)\.o[ :]*/\1.o $@ :/g' > $@

%.d : %.cc
	@$(MAKE_DEP) $^ 2>/dev/null | sed 's/\($*\)\.o[ :]*/\1.o $@ :/g' > $@

$(TESTS) : % : %.o
	$(LINKER) $(TESTFLAGS) -o $@ $< $(TESTLIBS)

$(TESTS) : $(OBJLIB)

$(SRCS) *.h : Makefile
	@touch $@

clean:  
	rm -rf *.o $(OBJLIB) $(OBJSO) *.d $(TESTS) 

install:
	rm -rf $(PREFIX)/lib/$(OBJLIB)
	rm -rf $(PREFIX)/include/$(SOFTNAME)/
	cp -i $(OBJLIB) $(PREFIX)/lib/
	cp -i $(OBJSO) $(PREFIX)/lib/
	mkdir -p $(PREFIX)/include/$(SOFTNAME)/
	cp -i *.h $(PREFIX)/include/$(SOFTNAME)/

dist:
	make clean
	cd ..; tar cvf $(SOFTNAME)_$(VERSION).tar $(SOFTNAME)/

.PHONY: all clean install 

